﻿var ActivatePopover = function (el) {
    el.popover({ trigger: 'hover', html: true, animation: true });
};

var RegisterDatePicker = function (el) {
    $(el).datepicker({
        showAnim: 'slideDown',
        changeMonth: true,
        changeYear: true,
        minDate: 0,
        maxDate: "+2Y",
        dateFormat: 'dd-MM-yy'
    });
};

var StartSpinner = function (el) {
    var spinner = $('#loadingspinner');
    var clone = spinner.clone();
    $(clone).removeClass('hide');
    $(el).prepend(clone);
};

var Highlight = function (el) {
    $(el).effect('highlight', { color: "#D2FFF6" }, 2000);
};

var ReWriteHtmlNoAnnimation = function (el, html, callback) {
    $(el).prepend(html);
    if (typeof (callback) == "function") callback();
};

var ReWriteHtml = function (el, html, callback) {
    $(el).fadeOut(500, function () {
        $(el).empty();
        $(el).prepend(html);
        $(el).fadeIn(500);
        Highlight(el);
        if (typeof (callback) == "function") callback();
    });
};

var StopSpinner = function (el) {
    $(el).empty();
};

var StartModalSpinner = function () {
    $('#loadingmodal').modal('show');

};

var StopModalSpinner = function () {
    $('#loadingmodal').modal('hide');
};

var GetHtml = function (url, reWriteElement, callback) {
    StartSpinner(reWriteElement);
    $.ajax({
        url: url,
        type: "GET",
        dataType: "html",
        success: function (result) {
            ReWriteHtml(reWriteElement, result, function () {
                if (typeof (callback) == "function") callback(result);
            });
        }
    });
};

var PostReturningHtml = function (url, data, reWriteElement, callback) {
    reWriteElement.fadeOut(100);
    reWriteElement.empty();
    StartSpinner(reWriteElement);
    reWriteElement.fadeIn(100);
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "html",
        success: function (result) {
            ReWriteHtml(reWriteElement, result, function () {
                if (typeof (callback) == "function") callback(result);
            });
        }
    });
};

var PostReturningHtmlNoAnnimation = function (url, data, reWriteElement, callback) {
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        dataType: "html",
        success: function (result) {
            reWriteElement.empty();
            ReWriteHtmlNoAnnimation(reWriteElement, result, function () {
                if (typeof (callback) == "function") callback(result);
            });
        }
    });
};

function AjaxTypeAhead(el, url, selectedCallback) {

    $(el).typeahead({
        source: function (query, process) {
            var data = { QueryString: query };
            $.ajax({
                url: url,
                type: 'POST',
                data: data,
                dataType: 'json',
                success: function (result) {
                    var names = [];
                    map = {};
                    $.each(result, function (i, item) {
                        map[item.Value] = item;
                        names.push(item.Value);
                    });
                    process(names);
                }
            });
        },
        sorter: function (items) {
            return items.sort();
        },
        updater: function (item) {
            var selectedId = map[item].Id;
            if (typeof (selectedCallback) == "function") {
                selectedCallback(selectedId);
            }
            return item;
        }
    });
}